package com.itita.ww2.data;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.tuple.Pair;

import com.smartfoxserver.v2.entities.data.ISFSArray;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;

public class LotteryConfig {
	public static boolean isLoaded = false;

	private static ISFSObject config;

	public static void init(ISFSObject data) {
		config = data.getSFSObject("config");
		isLoaded = true;
	}

	/**
	 * 获取对应指挥官等级 抽奖 消耗
	 */
	public static final ISFSObject getLotteryResourceByLv(int times, int lv) {
		return config.getSFSObject(String.valueOf(times)).getSFSArray("res").getSFSObject(lv - 1);
	}

	public static final List<Pair<LotteryItem, Number>> getLotteryJackpot(int lv) {
		List<Pair<LotteryItem, Number>> ref = new ArrayList<>();
		ISFSArray arr = config.getSFSArray("jackpot").getSFSArray(lv - 1);
		int size = arr.size();
		for (int i = 0; i < size; ++i) {
			ISFSObject obj = arr.getSFSObject(i);
			ref.add(Pair.of(new LotteryItem(obj.getInt("id"), obj.getUtfString("key"), obj.getInt("count")),
					obj.getInt("weight")));
		}
		return ref;
	}

	public static class LotteryItem {
		private String key;
		private int count;
		private int id;

		public LotteryItem(int id, String key, int count) {
			this.id = id;
			this.key = key;
			this.count = count;
		}

		public static ISFSObject convert2SFSObject(List<LotteryItem> items) {
			ISFSObject ref = SFSObject.newInstance();
			int size = items.size();
			for (int i = 0; i < size; ++i) {
				LotteryItem item = items.get(i);
				String key = item.getKey();
				if (ref.containsKey(key)) {
					ref.putInt(key, ref.getInt(key) + item.getCount());
				} else {
					ref.putInt(key, item.getCount());
				}
			}
			return ref;
		}

		public static List<Integer> convert2SFSArray(List<LotteryItem> items) {
			List<Integer> ref = new ArrayList<>();
			int size = items.size();
			for (int i = 0; i < size; ++i) {
				LotteryItem item = items.get(i);
				ref.add(item.getId());
			}
			return ref;
		}

		public String getKey() {
			return key;
		}

		public int getCount() {
			return count;
		}

		public int getId() {
			return id;
		}
	}
}
